package com.dinadi.yidi.ui.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;

import com.dinadi.yidi.R;



public class BootPageActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.fragment_2);
        boolean mFirst = isFirstEnter(BootPageActivity.this, BootPageActivity.this.getClass().getName());
        if (mFirst)
            mHandler.sendEmptyMessage(SWITCH_GUIDACTIVITY);
    }

    //****************************************************************
    // 判断应用是否初次加载，读取SharedPreferences中的guide_activity字段
    //****************************************************************
    private static final String SHAREDPREFERENCES_NAME = "my_pref";
    private static final String KEY_GUIDE_ACTIVITY = "guide_activity";

    @SuppressLint("WorldReadableFiles")
    @SuppressWarnings("deprecation")
    private boolean isFirstEnter(Context context, String className) {
        if (context == null || className == null || "".equalsIgnoreCase(className)) return false;
        String mResultStr = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_WORLD_READABLE)
                .getString(KEY_GUIDE_ACTIVITY, "");//取得所有类名 如 com.my.MainActivity
        return !mResultStr.equalsIgnoreCase("false");

    }

    //*************************************************
    // Handler:跳转至不同页面
    //*************************************************
    private final static int SWITCH_MAINACTIVITY = 1000;
    private final static int SWITCH_GUIDACTIVITY = 1001;
    @SuppressLint("HandlerLeak")
    public Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SWITCH_MAINACTIVITY:
                    Intent mIntent = new Intent();
                    mIntent.setClass(BootPageActivity.this, BootPageActivity.class);
                    BootPageActivity.this.startActivity(mIntent);
                    BootPageActivity.this.finish();
                    break;
                case SWITCH_GUIDACTIVITY:
                    //引导页
                    mIntent = new Intent();
                    mIntent.setClass(BootPageActivity.this, GuideActivity.class);
                    BootPageActivity.this.startActivity(mIntent);
                    BootPageActivity.this.finish();
                    break;
            }
            super.handleMessage(msg);
        }
    };


}
